﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using XConductor.Application.Seedwork.Services;
using XConductor.Domain.Seedwork.Capturing;
using XConductor.Domain.Seedwork.Capturing.Settings;
using XConductor.Domain.Seedwork.Playback;
using XConductor.Domain.Seedwork.Playback.Settings;

namespace Application.WP8.Services
{
    public class AudioService : IAudioService
    {
        private ICaptureSession m_session;
        private ICaptureSettingsService m_captureSettings;
        private IAudioPlayerSettingsService m_audioPlayerSettings;
        private IAudioPlayer m_player;

        public AudioService(ICaptureSession session, ICaptureSettingsService captureSettings, IAudioPlayer player, IAudioPlayerSettingsService audioPlayerSettings)
        {
            this.m_session = session;
            this.m_audioPlayerSettings = audioPlayerSettings;

            this.m_player = player;
            this.m_captureSettings = captureSettings;
        }

        public async Task StartRecording(string sourcePath)
        {
            ICaptureSettings settings = await this.m_captureSettings.AudioToFile(sourcePath);

            await this.m_session.Initialize(settings);
            await this.m_session.Start();
        }

        public async Task EndRecording()
        {
            await this.m_session.Stop();
        }

        public async Task StartPlaying(string sourcePath)
        {
            IAudioPlayerSettings settings = await this.m_audioPlayerSettings.PlayFromFile(sourcePath);

            await this.m_player.Initialize(settings);
            await this.m_player.Play();
        }

        public async Task PausePlaying()
        {
            this.m_player.Pause();
        }

        public async Task StopPlaying()
        {
            this.m_player.Stop();            
        }

        public async Task<bool> CanStartPlaying(string sourcePath)
        {
            IAudioPlayerSettings settings = await this.m_audioPlayerSettings.PlayFromFile(sourcePath);

            await this.m_player.Initialize(settings);

            return this.m_player.Initialized;
        }
    }
}
